package com.codingfly.controller;

import com.codingfly.controller.common.BaseController;
import com.codingfly.core.exception.BizException;
import com.codingfly.core.util.AssertUtils;
import com.codingfly.entity.SysDict;
import com.codingfly.model.Result;
import com.codingfly.model.dto.DictDto;
import com.codingfly.model.vo.DictItemVo;
import com.codingfly.service.SysDictItemService;
import com.codingfly.service.SysDictService;
import com.codingfly.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/dict")
public class DictController extends BaseController {
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysDictItemService sysDictItemService;
    @Autowired
    private CommonServiceImpl commonServiceImpl;

    @PostMapping
    public Result add(@RequestBody DictDto dto) {
        checkParams(dto, "code", "name", "auditTime");
        AssertUtils.isTrue(sysDictService.findByCode(dto.getCode())==null, "该CODE编码 "+dto.getCode()+" 已存在");
        SysDict sysDict = new SysDict();
        sysDict.setName(dto.getName());
        sysDict.setCode(dto.getCode());
        sysDict.setDescription(dto.getDescription());
        sysDict.setAuditTime(dto.getAuditTime());
        sysDict.setOrderNo(commonServiceImpl.queryNextOrderNoInt("sys_dict"));
        sysDictService.save(sysDict);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result add(@PathVariable("id") Long id, @RequestBody DictDto dto) {
        checkParams(dto, "code", "name", "auditTime");
        SysDict sysDict = Optional.ofNullable(sysDictService.findById(id)).orElseThrow(()->new BizException("该节点不存在或者已删除"));
        if (!dto.getCode().equals(sysDict.getCode())) {
            AssertUtils.isTrue(sysDictService.findByCode(dto.getCode())==null, "该CODE编码 "+dto.getCode()+" 已存在");
        }
        sysDict.setName(dto.getName());
        sysDict.setCode(dto.getCode());
        sysDict.setAuditTime(dto.getAuditTime());
        sysDict.setDescription(dto.getDescription());
        sysDictService.save(sysDict);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        AssertUtils.isTrue(!sysDictItemService.checkHasDictItem(id), "请先删除该分类下的字典表");
        sysDictService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<SysDict> queryById(@PathVariable("id") Long id) {
        return Result.success(sysDictService.findById(id));
    }

    @GetMapping("/{id}/dictItemTree")
    public Result<List<DictItemVo>> dictItemTree(@PathVariable("id") Long id) {
        return Result.success(sysDictService.findById(id));
    }

    @GetMapping("/dictItemTree")
    public Result<List<DictItemVo>> dictItemTree(@RequestParam("dictCode") String dictCode) {
        return ObjectUtils.isEmpty(dictCode)? Result.success(new ArrayList()):Result.success(sysDictService.dictItemTreeByCode(dictCode));
    }

    @GetMapping("/manyDictItemTree")
    public Result<Map<String, List<DictItemVo>>> manyDictItemTree(@RequestParam("dictCodes") List<String> dictCodes) {
        if (ObjectUtils.isEmpty(dictCodes)) {
            Result.success(new HashMap());
        }
        return Result.success(sysDictService.manyDictItemTree(dictCodes));
    }

    @PatchMapping("/move/{id}")
    public Result move(@PathVariable("id") Integer id, @RequestParam("action") String action) {
        if ("up".equals(action)) {
            commonServiceImpl.upOrderNoWithoutParent("sys_dict", id);
        } else if ("down".equals(action)) {
            commonServiceImpl.upOrderNoWithoutParent("sys_dict", id);
        }
        return Result.success();
    }


}
